package com.atguigu.tingshu.search.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.kafka.KafkaMessagePojo;
import com.atguigu.tingshu.search.service.ItemService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemApiController {

	@Autowired
	private ItemService itemService;

	/**
	 * 导入数据到es
	 */
	@GetMapping("/test")
	public Result test(){
		for(long i = 0; i < 1700; i++) {
			try {
				itemService.albumInfoFromMysqlToEs(i);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Result.ok();
	}

	@Autowired
	private KafkaTemplate kafkaTemplate;

	/**
	 * 造数据
	 * @return
	 */
	@GetMapping(value = "/kafka")
	public Result kafka(){
		for (long albumId = 1; albumId <= 30; albumId++) {
			for (int i = 1; i <= 3000; i++) {
				try {
					KafkaMessagePojo kafkaMessagePojo = new KafkaMessagePojo();
					kafkaMessagePojo.setAlbumId(albumId);
					int j = i % 4;
					switch (j){
						case 0 -> kafkaMessagePojo.setType("play");
						case 1 -> kafkaMessagePojo.setType("comment");
						case 2 -> kafkaMessagePojo.setType("buy");
						case 3 -> kafkaMessagePojo.setType("collect");
					}
					kafkaTemplate.send("tingshu_stream_rank_topic", JSONObject.toJSONString(kafkaMessagePojo));
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		}
		return Result.ok();
	}


}

